﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OnlineStore.Dal;

namespace OnlineStore.Bll
{
    public class bizCart:tbProduct
    {
        int _NumOrd;
        
        public int NumberOrder
        {
            get { return _NumOrd; }
            set { _NumOrd = value; }
        }
        public int Stock
        {
            get { return bizProduct.Stock(this); }
        }
        public double Sum
        {
            get { return Convert.ToDouble(this.PriceProduct) * _NumOrd; }
        }
        public static double SumAllList(List<bizCart> List)
        {
            if (List == null) return 0;
            double Sum = 0;
            foreach (bizCart i in List)
                Sum += i.Sum;
            return Sum;
        }
        public bizCart(tbProduct a)
        {
            this.ID = a.ID;
            this.Product_ID = a.Product_ID;
            this.Product_Name = a.Product_Name.Trim();
            this.Price = a.Price;
            this.Size = a.Size.Trim() ;
            this.Color = a.Color.Trim() ;
            this.Type = a.Type.Trim();
            this.Pur_Qty = a.Pur_Qty;
            this.Ord_Qty = a.Ord_Qty;
            this.Stock_Qty = a.Stock_Qty;
            this.Clock = a.Clock;
            this.Picture = a.Picture.Trim();
            this.Ord_Qty = a.Ord_Qty;
            this.PriceProduct = a.PriceProduct;
        }
        public List<bizCart> DelCart(List<bizCart> list,string ID)
        {
            int id = int.Parse(ID);
            foreach(bizCart i in list)
                if (i == id)
                {
                    list.Remove(i);
                    return list;
                }
            return list;
        }
        public bizCart()
        {
            // TODO: Complete member initialization
        }
        public List<bizCart> addToCart(List<bizCart> list, string ID, string NumberOrder)
        {
            if (ID == null) return list;
            int id = int.Parse(ID);
            if (list != null)
            {
                foreach (bizCart i in list)
                    if (i == id)
                    {
                        i._NumOrd += int.Parse(NumberOrder);
                        return list;
                    }
            }
            else
                list = new List<bizCart>();
            DalProduct _dalProduct = new DalProduct();
            bizCart _product = new bizCart(_dalProduct.Search(id));
            if ((object)_product ==null) return list;
            _product._NumOrd = int.Parse(NumberOrder);
            list.Add(_product);
            return list;
        }
        public bool CheckOut(tbCustomer User, List<bizCart> ListCart,DateTime Reviewed_Date,string Address,bizVoucher VourcherItem)
        {
            if ((object)User == null) return false;
            if (ListCart == null || ListCart.Count == 0) return true;
            DalOrder _dalOrder = new DalOrder();
            if (Reviewed_Date.Date < DateTime.Now) Reviewed_Date = DateTime.Now;
            if (VourcherItem.Values != 0)
            {
                VourcherItem.SetUsingVoucher();
                return _dalOrder.addOrder(ListCart, User, Reviewed_Date, Address, VourcherItem);
            }
            return _dalOrder.addOrder(ListCart, User, Reviewed_Date, Address);
            
        }
        public static bool operator ==(bizCart a, int ID)
        {
            return a.ID == ID;
        }
        public static bool operator !=(bizCart a, int ID)
        {
            return a.ID != ID;
        }
        public static bool operator ==(bizCart a, bizCart b)
        {
            return a.ID == b.ID;
        }
        public static bool operator !=(bizCart a, string ID)
        {
            return !(a==ID);
        }
        public static bool operator ==(bizCart a, string ID)
        {
            return a.ID == int.Parse(ID); ;
        }
        public static bool operator !=(bizCart a, bizCart b)
        {
            return a.ID != b.ID;
        }
        public static double PriceOrder(double Price)
        {
            if (Price >= 3000000) return Price * 95 / 100;
            return Price;
        }
    }
}